package org.cuatrovientos.erp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CityBOInterface;
import org.cuatrovientos.erp.model.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @author CARLOS
 *
 */
public class CityController {


	// Get log4j logger
	private static final Logger logger = Logger.getLogger(CityController.class);

	@Autowired
    private CityBOInterface cityBO;
    
        /**
         * default constructor
         */
        public CityController () {
               logger.info("City BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /shipment or /shipment/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment","/shipment/list"})
         */
        @RequestMapping({"/city","/city/list"})
        public ModelAndView cityDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<City> cityList = cityBO.selectAll();
                model.put("cities", cityList );
                return new ModelAndView("cityList", model);     
        }

        /**
         * handles /shipment/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/city/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<City> cityList = cityBO.search(description);
            model.put("cities", cityList );
            
            return new ModelAndView("CityList", model);     
        }

        /**
         * handles delete shipment, and redirect to shipment default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment","/shipment/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/city/delete"})
        public String deleteCity(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            cityBO.deleteById(id);
            return "redirect:/city";

        }
        
        /**
         * handles new shipment request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "shipment"
         * @return the name of the view to show
         * @RequestMapping({"/shipment/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/city/new"})
        public ModelAndView newCity() {
        		logger.info("new city ");
                City city = new City();
               
                return new ModelAndView("NewCity", "city", city);
        }
        
        /**
         * handles new shipment request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/shipment/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/city/save"})
        public String newCity(@Valid City city, BindingResult bindingResult) {
        		logger.info("new city " + city.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("city", city);
        			return "NewCity";
        		}
        		
        		// If data is ok, insert and go on
                cityBO.insert(city);
                return "redirect:/city";
        }
        
        /**
         * handles delete shipment, and redirect to shipment default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment","/shipment/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/city/detail"})
        public ModelAndView cityDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            City city = cityBO.selectById(id);
            model.put("city", city);
            
            return new ModelAndView("CityDetail",model);

        }
        
        /**
         * handles shipment update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/city/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                City city = cityBO.selectById(id);
                model.put("city", city);
               return new ModelAndView("UpdateCity",model);

        }
        
        /**
         * handles save update shipment request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/shipment/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/city/saveupdate"})
        public String saveUpdate(@Valid City city, BindingResult bindingResult) {
        		logger.info("update city " + city.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("city", city);
        			return "UpdateCity";
        		}
        		
                cityBO.update(city);
                return "redirect:/city";
        } 
        
    	/**
    	 * @param model
    	 * @return
    	 */
    	@RequestMapping({"/city/index"})
    	public String sayHelloPage(Map<String, Object> model) {
    		return "index/IndexCity";
    	}
}
